<?php
namespace App\Http\Controllers\Wechat;

use App\Http\Controllers\Controller as BaseController;
use App\Models\Member;
use App\Models\OtherLogin;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Session;
use Illuminate\Contracts\View\Factory as View;

/**
 * 微信端基础控制器
 *
 * @author Miluo Huang
 */
class Controller extends BaseController
{
    protected $appid = 'wx5bd0ee35cb4751d7';//'wx456c24d2ccc433c7';
    protected $appsecret = 'ad55522e026fc92f94e5d487d7aa2177';//'5b7281289ff05dfbcc49d30134aa30c5';

    public function __construct(Request $request,View $view)
    {
        $base_url = $request->fullUrl();
        $member = null;
        if (env('WECHAT_TEST')) {
            $member = Member::find(1);
            Session::put('user', $member);
            Session::put('wechat_user', ['openid' => 'test112233']);
        } else {
            $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" . $this->appid . "&redirect_uri=" . urlencode($base_url) . "&response_type=code&scope=snsapi_base&state=123#wechat_redirect";
            if (!$request->has('code') && !Session::has('wechat_user')) {
                //301跳转m端
                header('HTTP/1.1 302 Moved Permanently');
                header('Location: ' . $url);
                die;
            } else {
                if (!Session::has('wechat_user')) {
                    // 微信登录
                    $access_token = getWebAccessToken($this->appid, $this->appsecret, $request->input('code'));
//                    if (!env('APP_DEBUG')) {
//                        $wechat_user_info = getUserInfo($access_token['access_token'], $access_token['openid']);
//                        Session::put('wechat_user', $wechat_user_info);
//                    } else {
                        Session::put('wechat_user', $access_token);
//                    }
                    $other_login = OtherLogin::where('type', 2)->where('open_id', $access_token['openid'])->first();
                    if (!is_null($other_login)) {
                        $member = Member::find($other_login->member_id);
                        Session::put('user', $member);
                    }
                } else {
                    $wechat_user = Session::get('wechat_user');
                    if (!isset($wechat_user['openid'])) {
                        Session::forget('wechat_user');
                        //301跳转m端
                        header('HTTP/1.1 302 Moved Permanently');
                        header('Location: ' . $url);
                        die;
                    }
                }
//                else{
//                    $wechat_user_info = Session::get('wechat_user');
//                    $other_login = OtherLogin::where('type', 2)->where('open_id', $wechat_user_info['openid'])->first();
//                    if (!is_null($other_login)) {
//                        $member = Member::find($other_login->member_id);
//                        Session::put('user', $member);
//                    }
//                }
            }
        }

        $wechat = getSignPackage($this->appid, $this->appsecret);
        $share_url = is_null($member) ? $base_url : $base_url.'&uid='.md5($member->id);
        $view->share('wei_chat',$wechat);
        $view->share('share_url',$share_url);
        $view->share('share_uid',$request->input('uid'));

    }


}
